/*
 * @Description: In User Settings Edit
 * @Author: your name
 * @Date: 2019-09-05 08:57:21
 * @LastEditTime: 2019-09-09 17:04:59
 * @LastEditors: Please set LastEditors
 */
import React,{ Component } from "react";
import PropTypes from "prop-types"

// connect 负责连接组件 给redux里的数据放到组件中
// 普通函数的高阶组件
// export function connect(mapStateProps,mapDispatchProps){
//     return function (Wrapcomponent){
//         return class ConnectComponent extends Component{
//             render(){
//                 return (
//                     <div>
//                         Hello Word!
//                     </div>
//                 )
//             }
//         }
//     }
// }



export const connect = (mapStateProps=state=>state,mapDispatchProps={})=>(WrapComponent)=>{
    return class ConnectComponent extends Component{
        static contextTypes = {
            store:PropTypes.object
        }
        constructor(props,context){
            super(props,context);
            this.state={
                props:{}
            }
        }
        componentDidMount(){
            this.update();
        }
        update(){
            const {store} = this.context;
            const stateProps = mapStateProps(store.getState())
            this.setState({
                props:{
                    ...this.props,
                    ...stateProps,
                    ...mapDispatchProps
                }
            })
        }
        render(){
            return (
                <WrapComponent {...this.state.props}></WrapComponent>
            )
        }
    }
}
// Provider 把store 放到centext里 所有的子元素可以去到store
export class Provider extends Component{
    static childContextTypes = {
		store: PropTypes.object
	}
	getChildContext(){
		return {store:this.store}
	}
    constructor(props,context){
        super(props,context);
        this.store = props.store;
    }
    // getChildContext(){
    //     console.log(this.store)
    //     return {store:this.store}
    // }
    render(){
        return this.props.children;
    }
} 

















// console.log("++++++双层函数+++++++++")
    // const add = (num)=>num+3
    // 双层箭头函数 
    // const add = (x)=>(y)=>x+y+3 
    // 双层箭头函数  等于 两个嵌套函数
    // function add (x){
    //     return function (y){
    //         return x+y+3
    //     }
    // }

    // console.log("res is "+add(4)(2))

    // console.log("============")
    // console.log("+++++++++++在方法中使用对象展开符号++++++++++++");

    // function sayHello(...args){
    //     console.log(args);
    // }
    // sayHello("params1","params2")